1697B - Promo - CodeForces Solution


greedy sortings *900

Please click on ads to support us..

Python Code:

      d=[0]
a,b=map(int,input().split())
l=sorted((list(map(int,input().split()))),reverse=True)
s=0
v=0
for i in l:
    v+=i
    d.append(v)
for _ in range(b):
   n,m=map(int,input().split())
   print(d[n]-d[n-m])

C++ Code:

// Problem: B. Promo
// Contest: Codeforces - Educational Codeforces Round 130 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1697/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

#define ll long long


int main()
{
   ll n,q;
   
   cin>>n>>q;
   
   vector<ll> arr(n);
  
   
   for(ll i=0;i<n;i++)
   {
   	cin>>arr[i];
   }
    sort(arr.begin(),arr.end());
    
    for(ll i=n-2;i>=0;i--)
    {
    	arr[i]+=arr[i+1];
    }
   
   while(q--)
   {
   	ll a,b;
   	cin>>a>>b;
   	
   	ll s=n-a;
   	ll e=s+b;
   	
   	if(a==b){cout<<arr[s]<<endl;}
   	else{
   	
   	
   	
   	cout<<arr[s]-arr[e]<<endl;}
   	
   	
   }
}


Comments

Submit
0 Comments
More Questions

1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts
1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation
828A - Restaurant Tables
1735A - Working Week
1735D - Meta-set
1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections
1598C - Delete Two Elements
1400C - Binary String Reconstruction
1734D - Slime Escape
1499A - Domino on Windowsill
991A - If at first you don't succeed
1196C - Robot Breakout
373A - Collecting Beats is Fun
965A - Paper Airplanes